package cn.nb12.okhttp3.common;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;

public class SMS {

	public static boolean send(String code,String phone) {
	    String host = "http://sms.market.alicloudapi.com";
	    String path = "/singleSendSms";
	    String method = "GET";
	    String appcode = "ac1571ba39fc4994b7d13b587b9c47bc";
	    Map<String, String> headers = new HashMap<String, String>();
	    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
	    headers.put("Authorization", "APPCODE " + appcode);
	    Map<String, String> querys = new HashMap<String, String>();
	    querys.put("ParamString", " {\"code\":\""+code+"\"}");
	    querys.put("RecNum", phone);
	    querys.put("SignName", "大学生兼职网");
	    querys.put("TemplateCode", "SMS_70050133");


	    try {
	    	/**
	    	* 重要提示如下:
	    	* HttpUtils请从
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
	    	* 下载
	    	*
	    	* 相应的依赖请参照
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
	    	*/
	    	HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
	    	System.out.println(response.toString());
	    	//获取response的body
	    	System.out.println(EntityUtils.toString(response.getEntity()));
	    	Gson gson=new Gson();
	    	@SuppressWarnings("unchecked")
			Map<String,String> fromJson = gson.fromJson(EntityUtils.toString(response.getEntity()), Map.class);
	    	if(fromJson.get("success").equals("true")){
	    		return true;
	    	}
	    	else{
	    		return false;
	    	}
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
	    return false;
	}
	
	public static void main(String[] args) {
		send("4125", "13227769267");
	}

}
